package com.crcc.common.util;


/**
 * Created by xmj on 2021/1/21.
 */
public class MapUtil {
    /**
     * Krasovsky 1940 (北京54)椭球长半轴
     */
    private static double jzA  = 6378245.0;
    /**
     * 扁率
     * Krasovsky 1940 (北京54)椭球长半轴第一偏心率平方
     * 计算方式：
     * 长半轴：
     * a = 6378245.0
     * 扁率：
     * 1/f = 298.3（变量相关计算为：(a-b)/a）
     * 短半轴：
     * b = 6356863.0188 (变量相关计算方法为：b = a * (1 - f))
     * 第一偏心率平方:
     * e2 = (a^2 - b^2) / a^2;
     */
    private static double jzEE = 0.00669342162296594323;
    /***
     * 最大经度范围
     */
    private static double RANGE_LON_MAX = 137.8347;
    /**
     * 最小经度范围
     */
    private static double RANGE_LON_MIN = 72.004;
    /**
     * 最大纬度范围
     */
    private static double RANGE_LAT_MAX = 55.8271;
    /**
     * 最小纬度范围
     */
    private static double RANGE_LAT_MIN = 0.8293;
    public static boolean outOfChina(double lat, double lon) {
        if (lon < RANGE_LON_MIN || lon > RANGE_LON_MAX) {
            return true;
        }
        if (lat < RANGE_LAT_MIN || lat > RANGE_LAT_MAX) {
            return true;
        }
        return false;
    }

    private static final double LAT_OFFSET_0(double x, double y) {
        return -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
    }

    private static final double LAT_OFFSET_1(double x, double y) {
        return (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
    }

    private static final double LAT_OFFSET_2(double x, double y) {
        return (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
    }

    private static final double LAT_OFFSET_3(double x, double y) {
        return (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
    }

    private static final double LON_OFFSET_0(double x, double y) {
        return 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
    }

    private static final double LON_OFFSET_1(double x, double y) {
        return (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
    }

    private static final double LON_OFFSET_2(double x, double y) {
        return (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
    }

    private static final double LON_OFFSET_3(double x, double y) {
        return (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
    }

    public static double transformlat(double x, double y) {
        double ret = LAT_OFFSET_0(x, y);
        ret += LAT_OFFSET_1(x, y);
        ret += LAT_OFFSET_2(x, y);
        ret += LAT_OFFSET_3(x, y);
        return ret;
    }

    public static double transformlng(double x, double y) {
        double ret = LON_OFFSET_0(x, y);
        ret += LON_OFFSET_1(x, y);
        ret += LON_OFFSET_2(x, y);
        ret += LON_OFFSET_3(x, y);
        return ret;
    }
    /**
     * gg_lon 经度
     * gg_lat 纬度
     * GCJ-02转换BD-09
     * Google地图经纬度转百度地图经纬度
     **/
    public static double[] gcj02tobd09(double ggLat, double ggLon){
        double x = ggLon, y = ggLat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * Math.PI);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * Math.PI);
        double longitude = z * Math.cos(theta) + 0.0065;
        double latitude = z * Math.sin(theta) + 0.006;
        return new double[]{latitude,longitude};
    }
    /**
     * wgLon 经度
     * wgLat 纬度
     * BD-09转换GCJ-02
     * 百度转google
     **/
    public static double[] bd09togcj02(double bdLat, double bdLon){
        double x = bdLon - 0.0065, y = bdLat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * Math.PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math.PI);
        double longitude = z * Math.cos(theta);
        double latitude = z * Math.sin(theta);
        return new double[]{latitude,longitude};
    }

    /**
     * GCJ02(火星坐标系)转GPS84
     * @param bdLon:火星坐标系的经度
     * @param  bdLat:火星坐标系纬度
     * @return
     */
    public static double[] gcj02towgs84(double bdLat, double bdLon){
        if (outOfChina(bdLat, bdLon)) {
            return new double[]{bdLat, bdLon};
        }
        double dLat = transformlat(bdLon - 105.0, bdLat - 35.0);
        double dLon = transformlng(bdLon - 105.0, bdLat - 35.0);
        double radLat = bdLat / 180.0 * Math.PI;
        double magic = Math.sin(radLat);
        magic = 1 - jzEE * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((jzA * (1 - jzEE)) / (magic * sqrtMagic) * Math.PI);
        dLon = (dLon * 180.0) / (jzA / sqrtMagic * Math.cos(radLat) * Math.PI);
        double mgLat = bdLat + dLat;
        double mgLon = bdLon + dLon;
        return new double[]{bdLat-dLat,bdLon-dLon};
    }

    /**
     * WGS84转GCJ02(火星坐标系)
     * @param ggLon: WGS84坐标系的经度
     * @param ggLat:WGS84坐标系的纬度
     * @return
     */
    public static double[] wgs84togcj02(double ggLat, double ggLon){
        if (outOfChina(ggLat, ggLon)) {
            return new double[]{ggLat, ggLon};
        }
        double dLat = transformlat(ggLon - 105.0,ggLat - 35.0);
        double dLon = transformlng(ggLon - 105.0,ggLat - 35.0);
        double radLat = ggLat / 180.0 * Math.PI;
        double magic = Math.sin(radLat);
        magic = 1 - jzEE * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((jzA * (1 - jzEE)) / (magic * sqrtMagic) * Math.PI);
        dLon = (dLon * 180.0) / (jzA / sqrtMagic * Math.cos(radLat) * Math.PI);
        double mgLat = ggLat + dLat;
        double mgLon = ggLon + dLon;
        return new double[]{mgLat,mgLon};
    }

    /**
     * 百度 转 谷歌
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    public static String[] baiduToGoogle(double lng, double lat){
        double[] gcjData=bd09togcj02(lat,lng);
        double[] wgsData=gcj02towgs84(gcjData[0],gcjData[1]);
        return new String[]{format(wgsData[0]),format(wgsData[1])};
    }

    /**
     * 谷歌 转 百度
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    public static String[] googleToBaidu(double lng, double lat){
        double[] gcjData=wgs84togcj02(lat,lng);
        double[] bdResult=gcj02tobd09(gcjData[0],gcjData[1]);
        return new String[]{format(bdResult[0]),format(bdResult[1])};
    }

    /**
     * EPSG:3857转换经纬度(EPSG:4326)
     * @param lng
     * @param lat
     * @return
     */
    public static double[] mapboxToGoogleOffset(double lat,double lng){
        lng=lng-0.002452;
        lat=lat+0.005560;
        return new double[]{lat,lng};
    }

    /**
     * 经纬度(EPSG:4326)转换EPSG:3857
     * @param lng
     * @param lat
     * @return
     */
    public static double[]  googleToMapboxOffset(double lat,double lng){
        lng=lng + 0.002452;
        lat=lat - 0.005560;
        return new double[]{lat,lng};
    }

    /**
     *谷歌到百度，矫正偏移量
     * @param lat
     * @param lng
     * @return
     */
    public static double[]  googleToBaiduOffset(double lat,double lng){
        lng=lng + 0.006522;
        lat=lat + 0.005744;
        return new double[]{lat,lng};
    }

    /**
     * 百度到谷歌，矫正偏移量
     * @param lat
     * @param lng
     * @return
     */
    public static double[]  baiduToGoogleOffset(double lat,double lng){
        lng=lng - 0.006522;
        lat=lat - 0.005744;
        return new double[]{lat,lng};
    }

    /**
     * 取小数点后六位
     * @param num
     * @return
     */
    public static String format(double num){
        return String .format("%.6f", num);
    }


}